/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package net.thelostlands.shinycast.runnable;

import net.thelostlands.shinycast.Message;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;

/**
 *
 * @author fabio
 */
public class Mercury implements Runnable {
    String message;



    Message plugin;
    int seconds;
    String prefix = "[Server] ";
    int id;
    private boolean running;
    
    public Mercury(Message plugin){
        this.plugin=plugin;
        message="";
    }
    
    public String getMessage() {
        return message;
    }

    public void setMessage(String rawMessage) {
        this.message = rawMessage;
    }
    
    public String getPrefix() {
        return prefix;
    }

    public void setPrefix(String prefix) {
        this.prefix = prefix;
    }
    

    public boolean isRunning() {
        return running;
    }

    public void setRunning(boolean running) {
        this.running = running;
    }

    public int getSeconds() {
        return seconds;
    }

    public void setSeconds(int seconds) {
        this.seconds = seconds;
    }
    
    public void start(){
         if(seconds!=0){
             id=plugin.getServer().getScheduler().scheduleAsyncRepeatingTask(plugin, this, 0, seconds*20);             
         }
         else {
             plugin.getServer().getLogger().info("Non hai impostato la distanza tra un messaggio e l'altro, di default 60 s");
             seconds=60;
             id=plugin.getServer().getScheduler().scheduleAsyncRepeatingTask(plugin, this, 0, seconds*20);
         }
         running =true;
    }
    
    public void restart(){
        if(running) plugin.getServer().getScheduler().cancelTask(id);
        id=plugin.getServer().getScheduler().scheduleAsyncRepeatingTask(plugin, this, 0, seconds*20);

    }    
    
    @Override
    public void run() {

        String finalMessage = message.replaceAll("&([0-9a-f])", "\u00A7$1");

        for (Player player : plugin.getServer().getOnlinePlayers()) {

            player.sendMessage(ChatColor.DARK_RED + prefix+" "+finalMessage);
        }

    }

    public void stop() {
        plugin.getServer().getScheduler().cancelTask(id);
        running=false;
    }
}
